//
//  STEnchasePictureCollectionViewCell.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/30.
//

import UIKit

protocol STEnchaseTakePictureDelegate: NSObjectProtocol  {
    func handleEnchaseTakePhotoAction(_ index: Int) ->Void
}

class STEnchasePictureCollectionViewCell: UICollectionViewCell {
    
    class var identifier: String {
        get {
            return "STEnchasePictureCollectionViewCell"
        }
    }
    
    class func registerTo(_ collectionView: UICollectionView) {
        collectionView.register(STEnchasePictureCollectionViewCell.classForCoder(), forCellWithReuseIdentifier: STEnchasePictureCollectionViewCell.identifier)
    }
    
    class func reusableCell(_ collectionView: UICollectionView, indexPath: IndexPath) -> STEnchasePictureCollectionViewCell? {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: STEnchasePictureCollectionViewCell.identifier, for: indexPath) as? STEnchasePictureCollectionViewCell
        return cell
    }
    
    var delegate: STEnchaseTakePictureDelegate?
    
    var index: Int = -1
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        layer.cornerRadius = 12
        backgroundColor = UIColor.white

        addSubview(imgView)
        imgView.snp.makeConstraints { (make) in
            make.top.equalTo(16)
            make.centerX.equalTo(self)
            make.width.equalTo(136)
            make.height.equalTo(128)
        }
        
        imgView.addSubview(descLabel)
        descLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(-10)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(20)
        }
        
        addSubview(takeBtn)
        takeBtn.snp.makeConstraints { (make) in
            make.top.equalTo(imgView.snp.bottom).offset(12)
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.height.equalTo(30)
        }
        
        takeBtn.addTarget(self, action: #selector(takePhotoAction), for: UIControl.Event.touchUpInside)
        
    }
    
    func update(_ data: Dictionary<String, Any>, _ index: Int) {
        let defaultName = data["defaultName"] as! String
        let image = data["image"] as? UIImage
        imgView.image = image != nil ? image : UIImage.init(named: defaultName)
        self.index = index
        let status = data["status"] as! Int
        updateUploadState(status)
    }
    
    func updateUploadState(_ status: Int) {
        if(status == 1) {
            descLabel.text = "正在上传中..."
        } else if(status == 2) {
            descLabel.text = "已完成"
        } else {
            descLabel.text = nil
        }
    }
    
    @objc func takePhotoAction() {
        delegate!.handleEnchaseTakePhotoAction(index)
    }
    
    lazy var imgView: UIImageView = {
        let img = UIImageView.init()
        return img
    }()
    
    lazy var descLabel: UILabel = {
        let lb = UILabel.init()
        lb.textAlignment = .center
        lb.font = UIFont.systemFont(ofSize: 13)
        lb.textColor = UIColor.white
        return lb
    }()
    
    lazy var takeBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.backgroundColor = COLOR_HEX(hexString: "#008CFF", alpha: 1)
        btn.setTitle("拍照上传", for: .normal)
        btn.layer.cornerRadius = 5
        btn.setTitleColor(UIColor.white, for: UIControl.State.normal)
        return btn
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
